//
//  ViewController.m
//  SCNavTabbarDemo
//
//  Created by ShiCang on 14/11/17.
//  Copyright (c) 2014年 SCNavTabbarDemo. All rights reserved.
//

#import "ViewController.h"
#import "ArticlesViewController.h"
#import "DestinationsViewController.h"
#import "TripsViewController.h"

//#import "UserViewController.h"
#import "UserViewController.h"
#import "DetailViewController.h"
#import "LoginViewController.h"

#import "SearchViewController.h"
#import "Singleton.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)dealloc
{
    [_sView release];
    [_headView release];
    [_image release];
    [_buttonList release];
    [_detail release];
    [_TripsController release];
    [_DesController release];
    [_ArtController release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _detail = [[DetailViewController alloc] init];
    [self addChildViewController:_detail];
    [self.view addSubview:_detail.view];
    
    self.navigationController.navigationBar.translucent = NO;
    _sView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:_sView];
    
    
    //打印本地存储路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array firstObject];
    NSLog(@"path = %@", path);
    
 
#pragma mark-
#pragma mark 三个视图
    _TripsController = [[TripsViewController alloc] init];
    _TripsController.view.frame = CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40 - 64);
    [_sView addSubview:_TripsController.view];
    [self addChildViewController:_TripsController];
    [_TripsController release];
    
    _ArtController = [[ArticlesViewController alloc] init];
    _ArtController.view.frame = CGRectMake(self.view.frame.size.width, 40, self.view.frame.size.width, self.view.frame.size.height - 40 - 64);
    //        view2.backgroundColor = [UIColor whiteColor];
    [_sView addSubview:_ArtController.view];
    [_ArtController.view release];
    [self addChildViewController:_ArtController];
    [_ArtController release];
//
    _DesController = [[DestinationsViewController alloc] init];
    _DesController.view.frame = CGRectMake(self.view.frame.size.width * 2, 40, self.view.frame.size.width, self.view.frame.size.height - 40 - 64);
//    desController.view.backgroundColor = [UIColor orangeColor];
    [_sView addSubview:_DesController.view];
    [self addChildViewController:_DesController];
    [_DesController release];
    
    
    
    
    _headView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    _headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_headView];
    [_headView release];
    
    _image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 3, 40)];
    _image.image = [UIImage imageNamed:@"head.png"];
    [_headView addSubview:_image];
    [_image release];
    
    
    self.buttonTrip = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width / 3, 40)];
    [_buttonTrip setTitle:@"游记" forState:UIControlStateNormal];
    _buttonTrip.tag = 101;
    //    button.backgroundColor = [UIColor redColor];
//    [button setTintColor:[UIColor greenColor]];
    [_buttonTrip setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_buttonTrip addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:_buttonTrip];
    
    self.buttonArt = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3, 0, self.view.frame.size.width / 3, 40)];
    _buttonArt.tag = 102;
    [_buttonArt setTitle:@"专题" forState:UIControlStateNormal];
    [_buttonArt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_buttonArt addTarget:self action:@selector(buttonAction2:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:_buttonArt];
    
    
    self.buttonDes = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3 * 2, 0, self.view.frame.size.width / 3, 40)];
    _buttonDes.tag = 103;
    [_buttonDes setTitle:@"目的地" forState:UIControlStateNormal];
    [_buttonDes setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_buttonDes addTarget:self action:@selector(buttonAction3:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:_buttonDes];
    //    [sView release];
    self.buttonList = [NSArray arrayWithObjects:_buttonTrip, _buttonArt, _buttonDes, nil];
    
    
    //设置滚动范围
    _sView.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height);
    
    
    
    
    _sView.pagingEnabled = YES;
    //边界是否有反弹效果
    _sView.bounces = NO;
    //点击状态栏快速回到顶部
    _sView.scrollsToTop= YES;

    
    _sView.delegate = self;
    

    
    UIBarButtonItem *SearchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self  action:@selector(serachAction:)];
    UIBarButtonItem *userButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"user.png"] style:UIBarButtonItemStylePlain target:self action:@selector(userAction:)];
    UIBarButtonItem *detailButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting.png"] style:UIBarButtonItemStylePlain target:self action:@selector(detailAction:)];
    
//    UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:@"游·遇" style:UIBarButtonItemStylePlain target:self action:nil];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 80, 100)];
    label.textColor = [UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 alpha:1];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"游·遇";
    UIBarButtonItem *title1 = [[UIBarButtonItem alloc] initWithCustomView:label];
    [label release];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationItem  setRightBarButtonItems:[NSMutableArray arrayWithObjects: detailButton, userButton,SearchButton, nil]];
    [self.navigationItem setLeftBarButtonItem:title1];

    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 alpha:1];
}



- (void)buttonAction:(UIButton *)sender{

    _sView.contentOffset = CGPointMake(0, 0);
    _image.frame = CGRectMake(0, 0, self.view.frame.size.width / 3, 40);
    

    
}
- (void)buttonAction2:(id)sender{

    
    _sView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    _image.frame = CGRectMake(self.view.frame.size.width / 3, 0, self.view.frame.size.width / 3, 40);
    
}

- (void)buttonAction3:(id)sender{
    
    
    _sView.contentOffset = CGPointMake(self.view.frame.size.width * 2, 0);
    _image.frame = CGRectMake(self.view.frame.size.width / 3 * 2, 0, self.view.frame.size.width / 3, 40);

}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView.subviews objectAtIndex:1];
    
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / self.view.frame.size.width;
    //    _navTabBar.currentItemIndex = _currentIndex;
    
    if (index == 0) {
        _image.frame = CGRectMake(0, 0, self.view.frame.size.width / 3, 40);
        [_buttonTrip setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_buttonArt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_buttonDes setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
    }else if (index == 1){
        _image.frame = CGRectMake(self.view.frame.size.width / 3, 0, self.view.frame.size.width / 3, 40);
        [_buttonArt setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_buttonTrip setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_buttonDes setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    }else{
        _image.frame = CGRectMake(self.view.frame.size.width / 3 * 2, 0, self.view.frame.size.width / 3, 40);
        [_buttonDes setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_buttonTrip setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_buttonArt setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    }
    
    
}



#pragma mark-
#pragma mark 搜索
- (void)serachAction:(id)sender{
    SearchViewController *search = [[SearchViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:search];
    navi.navigationBar.translucent = NO;

    navi.navigationBar.alpha = 0.5;
    // UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(10, 30, 30, 30)];
    // back.backgroundColor = [UIColor yellowColor];
    // back.imageView.image = [UIImage imageNamed:@"back.png"];
    // [self.na addSubview:back];
    [self.navigationController presentViewController:navi animated:YES completion:^{
        
    }];
    
}

#pragma mark-
#pragma mark 用户登录
- (void)userAction:(id)sender{
    UserViewController *user = [[[UserViewController alloc] init] autorelease];
    Singleton *share = [Singleton shareInstance];
    if (share.logined == YES || [[share.loginDefault objectForKey:@"登陆状态"] isEqualToString:@"yes"]) {
        [self.navigationController pushViewController:user animated:NO];
    } else {
        LoginViewController *login = [[LoginViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
        nav.navigationBar.translucent = YES;
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
        nav.navigationBar.titleTextAttributes = dic;
        [self presentViewController:nav animated:YES completion:nil];
    }
}

#pragma mark-
#pragma mark 详情
- (void)detailAction:(id)sender{
    if (_detailJudge == NO) {
        
//        [self.view addSubview:_detail.view];
        [self.view bringSubviewToFront:_detail.view];
        _detailJudge = YES;
    }else{
                [self.view sendSubviewToBack:_detail.view];
        _detailJudge = NO;
//        [_detail.view removeFromSuperview];
    }
    
}

- (void)viewDidDisappear:(BOOL)animated{
    if (_detailJudge == YES) {
        [self.view sendSubviewToBack:_detail.view];
//        [_detail removeFromParentViewController];
        _detailJudge = NO;
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
